package com.easy.service.impl;

import com.easy.bean.Staff;
import com.easy.mapper.StaffDao;
import com.easy.service.StaffService;
import com.easy.util.ImageUtil;
import com.easy.util.Page;
import com.easy.util.passwordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    StaffDao staffDao;
    //新增数据
    @Override
    public Staff add(Staff staff) {
        staffDao.add(staff);
        //通过主键查询数据库中的数据
        Staff result=staffDao.one(staff.getId());
        return result;
    }

    @Override
    public int del(Integer id) {
        return staffDao.del(id);
    }

    //@CachePut(value = "staff", key = "#p0.id")
    @Override
    public Staff edit(Staff staff) {
        staffDao.edit(staff);
        Staff result=staffDao.one(staff.getId());
        return result;
    }

    @Override
    public List<Staff> all(Staff staff) {
        return staffDao.all(staff);
    }
    //@Cacheable(value = "staff", key = "#p0")
    @Override
    public Staff one(Integer id) {
        return staffDao.one(id);
    }

    @Override
    public void page(Page page, Staff staff) {
        int count = staffDao.pageCount(staff);
        Object data=staffDao.page(page, staff);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    public void cleanUnusedImages() {
        System.out.println("开始清理未被引用的OSS图片...");

        try {
            // 1. 获取数据库中所有员工的图片URL
            List<Staff> staffList = staffDao.all(new Staff());
            Set<String> dbImageUrls = new HashSet<>();
            for (Staff staff : staffList) {
                if (staff.getImage_url() != null && !staff.getImage_url().isEmpty()) {
                    dbImageUrls.add(staff.getImage_url());
                }
            }
            System.out.println("数据库中引用的图片数量: " + dbImageUrls.size());

            // 2. 获取OSS中所有图片URL
            List<String> ossImageUrls = ImageUtil.getAllImageUrls();
            System.out.println("OSS中图片总数: " + ossImageUrls.size());

            // 3. 找出未被引用的图片
            List<String> unusedImages = ossImageUrls.stream()
                    .filter(url -> !dbImageUrls.contains(url))
                    .collect(Collectors.toList());
            System.out.println("未被引用的图片数量: " + unusedImages.size());

            // 4. 删除未被引用的图片
            int deletedCount = 0;
            for (String unusedImage : unusedImages) {
                try {
                    String objectKey = ImageUtil.extractKeyFromUrl(unusedImage);
                    if (objectKey != null) {
                        ImageUtil.deleteImage(objectKey);
                        System.out.println("已删除未被引用的图片: " + unusedImage);
                        deletedCount++;
                    }
                } catch (Exception e) {
                    System.err.println("删除图片失败: " + unusedImage);
                    e.printStackTrace();
                }
            }

            System.out.println("清理完成，共删除 " + deletedCount + " 张未被引用的图片");
        } catch (Exception e) {
            System.err.println("清理未被引用的OSS图片时发生错误");
            e.printStackTrace();
        }
    }



}